<?php

require_once("include.php");
class periodstate extends head {
 function periodstate() {
  $this->table = "periodstate";
  $this->getmetadata();
  $this->maintainhist = true;
  $this->header[2] = "";
  $_SESSION["periodstate"] = $this;
  $this->lists[6] = $this->selectlisth("period","","f6",false);
  $this->lists[1] = $this->selectlisth("subledger","","f1",false);
  $this->lists[5] = $this->selectlisth(array(array("Y","Yes it is Open"),array("N","No it is Closed")),"","f5");
  $this->valids = array_fill(0,$this->atts,"");
 }
 
 function newcounter() {
  $ans =  getcounter("PE",1);
  return $ans;
 }
 
 function applyfromrequest() {
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,"");
  // Goes through request. Applies if legal, otherwise error.
 
  if ($f=@$_REQUEST["f5"]) {
   // Y or N if open or closed.
   if ($f == "Y" or $f == "N") {
    $this->header[5] = $f; 
   } else {
    $this->valids[5] = "Please enter a Y or an N";
   }
  }
 
  $this->valid = every($this->valids,"");
  
 }
 
 function showform() {
  $readonly = "readonly"; $focalpoint = "f1";

  if ($this->header[2] == "0") { $focalpoint = "f0"; $readonly = "";} 
 
  echo "<head>";
  echo SCRIPTS;
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
  echo "<h1>Open and close periods</h1>"; 
  echo "<form name='form' method=POST>";
  echo "<input type=hidden name=res value=''>";
  echo "<table>";
  echo "<tr><td>Subledger</td><td>".$this->header[1]."</td></tr>";
  echo "<tr><td>Period</td><td>".$this->header[6]."</td></tr>";
  echo "<tr><td></td></tr>";
  echo $this->htmlrow("5");
  echo "</table>";
  echo "<input class=bt type=submit name=sub value=Save>";
  echo "<input class=bt type=submit name=sub value=Apply>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>";
  echo "</form>"; 
  // set up help
  echo $this->registerhelp();
  $this->gethelp();
 }

 function showchoose() {
  echo "<h1>Choose Ledger / Period</h1>";
  echo "<form>";
  echo "<table>";
  echo $this->htmlrow("6","Choose Period");
  echo $this->htmlrow("1","Choose Ledger");
  echo "</table>";
  echo "<input class=bt type=submit name=sub value=Continue>"; 
  echo "</form>";
 }
 
 function readdb() {
   $periodid = $_REQUEST["f6"];
   $subledgerid = $_REQUEST["f1"];
   $ans = sqlread("select * from $this->table where periodid = '$periodid' and subledgerid = '$subledgerid'");
   if ($ans == "") {
    $ans = array("",$subledgerid,"0","","","",$periodid);
   }
   $this->header = $ans;
   $this->applyrefs();
   if ($this->refs[1] != "" && $this->refs[6] != "") {
    $this->origheader = $ans;
    $this->setdisabled();
    return true ;
   }
   unset($this->header);
   $this->header[2] = "";
   return false;
 }
 
 function applyrefs() {
  $this->refs = array_fill(0,$this->atts,"");
	$desc = freaditem("subledger",$this->header[1]); $this->refs[1] = $desc[1];
	$desc = freaditem("period",$this->header[6]); $this->refs[6] = $desc[1];
 }
 
 
}
?>